<?php

/*
 * This class is used for as Model for dropdown (tbl_list) table 
 */

class Model_List extends Model_Base {

    protected $_name = 'tbl_list';
    protected $_primaryKey = 'child_id';

    /**
     *  This function is used to get contractors
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchall($parentid) {
       
        $select = $this->select()
                ->from(array('con' => 'tbl_list'))
                ->where('child_status != 2')
                ->where('parent_id = ' . $parentid)
                ->order('child_name asc');
        $data = parent::fetchAll($select);
        #echo $select;exit;
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }
    
    
    public function fetchlistbyparent($parentid) {
       
        $select = $this->select()
                ->from(array('con' => 'tbl_list'))
                ->where('child_status != 2')
                ->where('parent_id = ' . $parentid)
                ->where('lang_id = 17')
                ->order('child_name asc');
        $data = parent::fetchAll($select);
        
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    public function fetchEntryByViewId($id) {
        $select = $this->select()
                ->from(array('prd' => 'tbl_list'))
                ->setIntegrityCheck(false)
                ->join(array('lgn' => 'tbl_login'), "lgn.lgn_id = prd.updated_lgn_id")
                ->join(array('parent' => 'tbl_parent_list'), "parent.parent_id = prd.parent_id")
                ->join(array('lang' => 'tbl_language'), "lang.lang_id = prd.lang_id")
                ->where('child_id = ?', $id)
                ->where('child_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        #echo $select;exit;
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /*     * ********Fetch all data from parent table************* */

    public function fetchParentListtype($where = "", $limit = "", $offset = "0", $order = 'parent_id', $sort = 'ASC') {
        //parentlist model
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchParentListtype($where = "", $limit = "", $offset = "0", $order = 'parent_id', $sort = 'ASC');
        return $ParentdropdownArray;
    }

    public function fetchParentListCount($where) {
        //parentlist model
        $ParentdropdownModel = new Model_Parentlist();
        $ParentdropdownArray = $ParentdropdownModel->fetchParentListCount($where);
        return $ParentdropdownArray;
    }

    //get parent list for dropdown data 
    public function getparentArray() {
        $parentModel = new Model_Parentlist();
        $parentArray = $parentModel->fetchall('');
        $finalArray[''] = '-- Select Dropdown Name --';
        foreach ($parentArray as $com) {
            $finalArray[$com['parent_id']] = $com['parent_name'];
        }
        return $finalArray;
    }

    //get language list
    public function getlanguageArray() {
        $languageModel = new Model_Language();
        $languageArray = $languageModel->language_fetchall();
        $finalArray[''] = '';
        foreach ($languageArray as $com) {
            $finalArray[$com['lang_id']] = $com['lang_name'];
        }
        return $finalArray;
    }

    public function getStatusArray() {
        return array(
           // '' => '-- Select Status --',
            '1' => 'Active',
            '0' => 'Inactive'
        );
    }

    /**
     * Fetch an individual entry
     *
     * @param  int|string $id
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchEntryById($id) {
        $select = $this->select()
                ->from(array('prd' => 'tbl_list'))
                ->where('child_id = ?', $id)
                ->where('child_status != 2');
        // see reasoning in fetchEntries() as to why we return only an array
        $data = $this->fetchRow($select);
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get dropdown list
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function fetchType($where = "", $limit = "", $offset = "0", $order = 'child_id', $sort = 'ASC') {
        $select = $this->select()
                ->from(array('child' => 'tbl_list'))
                ->setIntegrityCheck(false)
                ->join(array('parent' => 'tbl_parent_list'), "child.parent_id = parent.parent_id")
                ->join(array('ln' => 'tbl_language'), "ln.lang_id = child.lang_id",array('lang_name'))
                ->where('child_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($limit != "") {
            $select->limit($limit, $offset);
        }

        $select->order($order . " " . $sort);
        #echo $select; exit;
        if ($where != '') {
            $data = parent::fetchAll($select);
        }
        if (!empty($data)) {
            return $data->toArray();
        } else {
            return array();
        }
    }

    /**
     *  This function is used to get total rows
     * @return int
     */
    public function getTypeCount($where) {
        $select = $this->select()
                ->from(array('child' => 'tbl_list'), Array('cnt' => 'count(*)'))
                ->setIntegrityCheck(false)
                ->join(array('parent' => 'tbl_parent_list'), "child.parent_id = parent.parent_id")
                ->where('child_status != 2');
        if ($where != '') {
            $select->where($where);
        }
        if ($where != '') {
            $rows = parent::fetchRow($select);

            return($rows->cnt);
        }
    }

    /**
     *  This function is used to delete companys
     * @return null|Zend_Db_Table_Row_Abstract
     */
    public function deleteType($prdtArray) {
        $loginModel = new Model_Login();
        if (is_array($prdtArray)) {

            foreach ($prdtArray as $_company) {
                $companyData = Array();
                $companyData = $this->fetchEntryById($_company);
                if (count($companyData) > 0) {
                    $data = array('child_status' => '2');
                    $this->update($data, 'child_id =' . (int) $_company);
                }
            }
        } else {
            $child_id = $prdtArray;
            $companyData = $this->fetchEntryById($child_id);
            if (count($companyData) > 0) {
                $data = array('child_status' => '2');
                $this->update($data, 'child_id =' . $child_id);
            }
        }
    }

    
    //get parent list for dropdown data 
    public function getrefchildArray($parentid = '') {
        
        $childArray = $this->fetchlistbyparent($parentid);
        
        $finalArray[''] = '-- Select Reference Name --';
        foreach ($childArray as $com) {
            $finalArray[$com['child_id']] = $com['child_name'];
        }
        return $finalArray;
    }
    
    
}
